<?php
/**
 * SGoodsBrowseModel.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\model;

use think\Db;

class SGoodsBrowseModel
{
    /**
     * 自增id
     */
    protected $id = '';
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    
    /**
     * 添加时间
     */
    protected $create_ts = '0';
    public function getCreateTs()
    {
        return $this->create_ts;
    }
    public function setCreateTs($create_ts)
    {
        $this->create_ts = $create_ts;
    }
    
    /**
     * 商品id
     */
    protected $goods_id = '0';
    public function getGoodsId()
    {
        return $this->goods_id;
    }
    public function setGoodsId($goods_id)
    {
        $this->goods_id = $goods_id;
    }
    
    /**
     * 会员id
     */
    protected $member_id = '0';
    public function getMemberId()
    {
        return $this->member_id;
    }
    public function setMemberId($member_id)
    {
        $this->member_id = $member_id;
    }
    
    /**
     * constructor.
     * @param array $data
     */
    public function __construct($data=[])
    {

        // 自增id
        if(isset($data['id']))
        {
            $this->id = $data['id'];
        }

        // 添加时间
        if(isset($data['create_ts']))
        {
            $this->create_ts = $data['create_ts'];
        }

        // 商品id
        if(isset($data['goods_id']))
        {
            $this->goods_id = $data['goods_id'];
        }

        // 会员id
        if(isset($data['member_id']))
        {
            $this->member_id = $data['member_id'];
        }

    }
    
    /**
     * 获取分页信息
     * @param $where
     * @param array $extend
     * @return \think\Paginator
     */
    static public function paginate($where,$extend=[])
    {
        $handler = Db::table('s_goods_browse');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->paginate();
    }
    
    /**
     * 添加|修改
     * @param $flag
     * @param $data
     * @param array $extend
     * @return int|string
     */
    static public function save($flag,$data,$extend=[])
    {
        if(empty($flag))
        {
            // 添加
            $data['create_ts'] = time();
            return Db::table('s_goods_browse')
                ->insertGetId($data);
        }
        else
        {
            // 编辑
            $data['update_ts'] = time();
            return Db::table('s_goods_browse')
                ->where('id',$flag)
                ->update($data);
        }
    }
    
    /**
     * 获取详情
     * @param $where
     * @param array $extend
     * @return array|false|null|\PDOStatement|string|\think\Model
     */
    static public function get($where,$extend=[])
    {
        $handler = Db::table('s_goods_browse');
        if(!empty($extend['field']))
        {
            $handler->field($extend['field']);
        }
        if(!empty($extend['order']))
        {
            $handler->order($extend['order']);
        }
        if(!empty($extend['group']))
        {
            $handler->group($extend['group']);
        }
        return $handler
            ->where($where)
            ->find();
    }
    
    /**
     * 删除
     * @param $flag
     * @param array $extend
     * @return int
     */
    static public function delete($flag,$extend=[])
    {
        return Db::table('s_goods_browse')
            ->where('id',$flag)
            ->delete();
    }
}